﻿#region imports

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.ProviderServices;
using CondeNet.AssetManager.ProviderServices.Data;
using CondeNet.AssetManager.Business.Interface;
using CondeNet.AssetManager.DataProvider.Sql.Mappers;

using CondeNet.AssetManager.Business;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql
{
    public class MSSqlProvider : IAssetReaderFactory , IAssetWriterFactory
    {
        #region privates

        private AssetManagerDBDataContext context;

        #endregion

        #region constructors

        public MSSqlProvider ( ) { }

        #endregion

        #region IAssetReaderFactory Members

        // Asset
        public IAsset GetAssetByID ( int assetId )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return AssetMapper.ToBusinessObject((from ast in context.T_Assets where ast.Asset_IDPK==assetId select ast).Single()); // ( ( T_Asset ) context.GetAssetByID ( assetId ).SingleOrDefault ( ) );
            }
        }
        //public IList<IAsset> GetAllAsset ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetMapper.ToBusinessObjects ( ( List<T_Asset> ) context.GetAllAsset ( ).ToList ( ) );
        //    }
        //}
        ///// <summary>
        ///// Gets Assets which have related AssetContent in the specified langaugeId
        ///// 
        ///// Will return an Asset which has AssetContent, AssetMetaData and 
        ///// AssetContentMetaData of the specified langaugeId
        ///// </summary>
        ///// <param name="id">ID ( ID of the desired Asset )</param>
        ///// <param name="languageId">LanguageID ( ID of the desired language )</param>
        ///// <returns>An Asset if it has AssetContent with the desired language</returns>
        //public IAsset GetAssetByIDLanguageID ( int id , int languageId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        // get an Asset that is known to have the desired language in it's AssetContent
        //        IAsset asset = AssetMapper.ToBusinessObject ( ( T_Asset ) context.GetAssetByIDLanguageID ( id , languageId ).SingleOrDefault ( ) );

        //        // refactor this using LINQ and Lambda expressions..

        //        // make new lists
        //        List<IAssetMetaData> assetMetaDataList = new List<IAssetMetaData> ( );
        //        List<IAssetContent> assetContentList = new List<IAssetContent> ( );
        //        List<IAssetContentMetaData> assetContentMetaDataList;

        //        // thin out the info that doesn't have the same languageId
        //        foreach ( IAssetMetaData item in asset.AssetMetaDataList )
        //        {
        //            if ( item.MetaData.MetaValue.LanguageID == languageId ) assetMetaDataList.Add ( item );
        //        } 
        //        asset.AssetMetaDataList = assetMetaDataList;

        //        foreach ( IAssetContent item in asset.AssetContentList )
        //        {
        //            if ( item.LanguageID == languageId ) assetContentList.Add ( item );

        //            assetContentMetaDataList = new List<IAssetContentMetaData> ( );

        //            foreach ( IAssetContentMetaData subitem in item.AssetContentMetaDataList )
        //            {
        //                if ( subitem.MetaData.MetaValue.LanguageID == languageId ) assetContentMetaDataList.Add ( subitem );
        //            } 
        //            item.AssetContentMetaDataList = assetContentMetaDataList;
        //        } 
        //        asset.AssetContentList = assetContentList;

        //        return asset;
        //    }
        //}
        // AssetContent
        public IAssetContent GetAssetContentByID ( int assetContentId )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return AssetContentMapper.ToBusinessObject((from cnt in context.T_AssetContents where cnt.AssetContent_IDPK == assetContentId select cnt).Single());  //( T_AssetContent ) context.GetAssetContentByID ( assetContentId ).SingleOrDefault ( ) );
            }
        }
        public IList<IAssetContent> GetAssetContentByAssetID ( int assetId )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return AssetContentMapper.ToBusinessObjects((from cnt in context.T_AssetContents where cnt.AssetID_FK == assetId select cnt).ToList()); //( List<T_AssetContent> ) context.GetAssetContentByAssetID ( assetId ).ToList ( ) );
            }
        }
        //public IList<IAssetContent> GetAllAssetContent ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetContentMapper.ToBusinessObjects ( ( List<T_AssetContent> ) context.GetAllAssetContent ( ).ToList ( ) );
        //    }
        //} // - modified
        //// AssetContentMetaData
        //public IList<IAssetContentMetaData> GetAssetContentMetaDataByAssetContentID ( int assetContentId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetContentMetaDataMapper.ToBusinessObjects ( ( List<T_AssetContentMetaData> ) context.GetAssetContentMetaDataByAssetContentID ( assetContentId ).ToList ( ) );
        //    }
        //}// - modified ( taken List out of name)
        //public IList<IAssetContentMetaData> GetAllAssetContentMetaData ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetContentMetaDataMapper.ToBusinessObjects ( ( List<T_AssetContentMetaData> ) context.GetAllAssetContentMetaData ( ).ToList ( ) );
        //    }
        //} // - modified
        //public IAssetContentMetaData GetAssetContentMetaDataByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetContentMetaDataMapper.ToBusinessObject ( ( T_AssetContentMetaData ) context.GetAssetContentMetaDataByID ( id ).SingleOrDefault ( ) );
        //    }
        //} // - modified
        //// AssetContentRepository 
        //public IList<IAssetContentRepository> GetAssetContentRepositoryListByAssetContentID ( int assetContentId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetContentRepositoryMapper.ToBusinessObjects ( ( List<T_AssetContentRepository> ) context.GetAssetContentRepositoryByAssetContentID ( assetContentId ).ToList ( ) );
        //    }
        //}
        //public IAssetContentRepository GetAssetContentRepositoryByID ( int Id )
        //{
        //    return AssetContentRepositoryMapper.ToBusinessObject ( ( T_AssetContentRepository ) context.GetAssetContentRepositoryByID ( Id ).SingleOrDefault ( ) );
        //}
        //public IList<IAssetContentRepository> GetAllAssetContentRepository ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetContentRepositoryMapper.ToBusinessObjects ( ( List<T_AssetContentRepository> ) context.GetAllAssetContentRepository ( ).ToList ( ) );
        //    }
        //}
        //public IList<IAssetContentRepository> GetAssetContentRepositoryByAssetContentID ( int assetContentId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetContentRepositoryMapper.ToBusinessObjects ( ( List<T_AssetContentRepository> ) context.GetAssetContentRepositoryByAssetContentID ( assetContentId ).ToList ( ) );
        //    }
        //}
        // AssetContentType
        public IList<IAssetContentType> GetAllAssetContentType ( )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return AssetContentTypeMapper.ToBusinessObjects(context.T_AssetContentTypes.Select(cnt => cnt).ToList()); //( List<T_AssetContentType> ) context.GetAllAssetContentType ( ).ToList ( ) );
            }
        }
        public IAssetContentType GetAssetContentTypeByID ( int id )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return AssetContentTypeMapper.ToBusinessObject((from cnt in context.T_AssetContentTypes where cnt.AssetContentType_IDPK == id select cnt).Single()); //( T_AssetContentType ) context.GetAssetContentTypeByID ( id ).SingleOrDefault ( ) );
            }
        }
        // AssetMetaData
        //public IAssetMetaData GetAssetMetaDataByID ( int assetMetaDataId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetMetaDataMapper.ToBusinessObject ( ( T_AssetMetaData ) context.GetAssetMetaDataByID ( assetMetaDataId ).SingleOrDefault ( ) );
        //    }
        //}
        //public IList<IAssetMetaData> GetAllAssetMetaData ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetMetaDataMapper.ToBusinessObjects ( ( List<T_AssetMetaData> ) context.GetAllAssetMetaData ( ).ToList ( ) );
        //    }
        //}
        //public IList<IAssetMetaData> GetAssetMetaDataByAssetID ( int assetId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetMetaDataMapper.ToBusinessObjects ( ( List<T_AssetMetaData> ) context.GetAssetMetaDataByAssetID ( assetId ).ToList ( ) );
        //    }
        //}
        //public IList<IAssetMetaData> GetAssetMetaDataByAssetIDStatusID ( int assetId , int statusId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetMetaDataMapper.ToBusinessObjects ( ( List<T_AssetMetaData> ) context.GetAssetMetaDataByAssetIDStatusID ( assetId , statusId ).ToList ( ) );
        //    }
        //}
        //public IList<IAssetMetaData> GetAssetMetaDataByAssetIDLanguageID ( int assetId , int languageId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetMetaDataMapper.ToBusinessObjects ( ( List<T_AssetMetaData> ) context.GetAssetMetaDataByAssetIDLanguageID ( assetId , languageId ).ToList ( ) );
        //    }
        //}
        // AssetRightsUsage
        public IList<IAssetRightsUsage> GetAllAssetRightsUsage ( )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return AssetRightsUsageMapper.ToBusinessObjects(context.T_AssetRightsUsages.Select(cnt => cnt).ToList()); //( List<T_AssetRightsUsage> ) context.GetAllAssetRightsUsage ( ).ToList ( ) );
            }
        }
        //public IAssetRightsUsage GetAssetRightsUsageByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetRightsUsageMapper.ToBusinessObject ( ( T_AssetRightsUsage ) context.GetAssetRightsUsageByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        //public IList<IAssetRightsUsage> GetAssetRightsUsageByAssetID ( int assetId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssetRightsUsageMapper.ToBusinessObjects ( ( List<T_AssetRightsUsage> ) context.GetAssetRightsUsageByAssetID ( assetId ).ToList ( ) );
        //    }
        //}
        //// AssociatedContent
        //public IList<IAssociatedContent> GetAllAssociatedContent ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssociatedContentMapper.ToBusinessObjects ( ( List<T_AssociatedContent> ) context.GetAllAssociatedContent ( ).ToList ( ) );
        //    }
        //}
        //public IAssociatedContent GetAssociatedContentByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssociatedContentMapper.ToBusinessObject ( ( T_AssociatedContent ) context.GetAssociatedContentByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        //public IList<IAssociatedContent> GetAssociatedContentByAssetID ( int assetId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssociatedContentMapper.ToBusinessObjects ( ( List<T_AssociatedContent> ) context.GetAssociatedContentByAssetID ( assetId ).ToList ( ) );
        //    }
        //}
        //// AssociatedContentType
        //public IList<IAssociatedContentType> GetAllAssociatedContentType ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssociatedContentTypeMapper.ToBusinessObjects ( ( List<T_AssociatedContentType> ) context.GetAllAssociatedContentType ( ).ToList ( ) );
        //    }
        //}
        //public IAssociatedContentType GetAssociatedContentTypeByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return AssociatedContentTypeMapper.ToBusinessObject ( ( T_AssociatedContentType ) context.GetAssociatedContentTypeByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        // Language
        public IList<ILanguage> GetAllLanguage ( )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return LanguageMapper.ToBusinessObjects(context.T_Languages.Select(cnt => cnt).ToList()); // ( List<T_Language> ) context.GetAllLanguage ( ).ToList ( ) );
            }
        }
        public ILanguage GetLanguageByID ( short id )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return LanguageMapper.ToBusinessObject((from lang in context.T_Languages where lang.Language_IDPK == id select lang).Single()); //( T_Language ) context.GetLanguageByID ( id ).SingleOrDefault ( ) );
            }
        }
        //// MetaData - integrated
        //public IMetaData GetMetaDataByID ( int metaDataId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return MetaDataMapper.ToBusinessObject ( ( T_MetaData ) context.GetMetaDataByID ( metaDataId ).SingleOrDefault ( ) );
        //    }
        //}
        //public IList<IMetaData> GetAllMetaData ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        //return BuildMetaDataList ( MetaDataMapper.ToBusinessObjects ( ( List<T_MetaData> ) context.GetAllMetaData ( ).ToList ( ) ) );
        //        return MetaDataMapper.ToBusinessObjects ( ( List<T_MetaData> ) context.GetAllMetaData ( ).ToList ( ) );
        //    }
        //}
        //public IList<IMetaData> GetMetaDataByLanguageID ( int languageId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        //return BuildMetaDataList ( MetaDataMapper.ToBusinessObjects ( ( List<T_MetaData> ) context.GetMetaDataByMetaValueLanguageID ( languageId ).ToList ( ) ) );
        //        return MetaDataMapper.ToBusinessObjects ( ( List<T_MetaData> ) context.GetMetaDataByMetaValueLanguageID ( languageId ).ToList ( ) );
        //    }
        //}
        //public IList<IMetaData> GetMetaDataByMetaValueLanguageID ( int languageId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        //return BuildMetaDataList ( MetaDataMapper.ToBusinessObjects ( ( IList<T_MetaData> ) context.GetMetaDataByMetaValueLanguageID ( languageId ).ToList ( ) ) );
        //        return MetaDataMapper.ToBusinessObjects ( ( IList<T_MetaData> ) context.GetMetaDataByMetaValueLanguageID ( languageId ).ToList ( ) );
        //    }
        //}
        // MetaKey
        public IList<IMetaKey> GetAllMetaKey ( )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return MetaKeyMapper.ToBusinessObjects(context.T_MetaKeys.Select(key => key).ToList());// ( List<T_MetaKey> ) context.GetAllMetaKey ( ).ToList ( ) );
            }
        }
        //public IMetaKey GetMetaKeyByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return MetaKeyMapper.ToBusinessObject ( ( T_MetaKey ) context.GetMetaKeyByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        //// MetaValue
        //public IList<IMetaValue> GetAllMetaValue ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return MetaValueMapper.ToBusinessObjects ( ( List<T_MetaValue> ) context.GetAllMetaValue ( ).ToList ( ) );
        //    }
        //}
        //public IMetaValue GetMetaValueByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return MetaValueMapper.ToBusinessObject ( ( T_MetaValue ) context.GetMetaValueByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        // MimeType
        public IList<IMimeType> GetAllMimeType ( )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return MimeTypeMapper.ToBusinessObjects(context.T_MimeTypes.Select(key => key).ToList()); //( List<T_MimeType> ) context.GetAllMimeType ( ).ToList ( ) );
            }
        }
        //public IMimeType GetMimeTypeByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return MimeTypeMapper.ToBusinessObject ( ( T_MimeType ) context.GetMimeTypeByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        //// RelatedAsset
        //public IList<IRelatedAsset> GetAllRelatedAsset ( )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return RelatedAssetMapper.ToBusinessObjects ( ( List<T_RelatedAsset> ) context.GetAllRelatedAsset ( ).ToList ( ) );
        //    }
        //}
        //public IRelatedAsset GetRelatedAssetByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return RelatedAssetMapper.ToBusinessObject ( ( T_RelatedAsset ) context.GetRelatedAssetByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        //public IList<IRelatedAsset> GetRelatedAssetByAssetID ( int assetId )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return RelatedAssetMapper.ToBusinessObjects ( ( List<T_RelatedAsset> ) context.GetRelatedAssetByAssetID ( assetId ).ToList ( ) );
        //    }
        //}
        // Repository
        public IList<IRepository> GetAllRepository ( )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return RepositoryMapper.ToBusinessObjects(context.T_Repositories.Select(key => key).ToList()); //( List<T_Repository> ) context.GetAllRepository ( ).ToList ( ) );
            }
        }
        //public IRepository GetRepositoryByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return RepositoryMapper.ToBusinessObject ( ( T_Repository ) context.GetRepositoryByID ( id ).SingleOrDefault ( ) );
        //    }
        //}
        // Status
        public IList<IStatus> GetAllStatus ( )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                return StatusMapper.ToBusinessObjects(context.T_Status.Select(key => key).ToList()); // ( List<T_Status> ) context.GetAllStatus ( ).ToList ( ) );
            }
        }
        //public IStatus GetStatusByID ( int id )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        return StatusMapper.ToBusinessObject ( ( T_Status ) context.GetStatusByID ( id ).SingleOrDefault ( ) );
        //    }
        //}

        #endregion

        #region IAssetWriterFactory Members

        #region add methods

        public void AddAsset ( IAsset entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Asset asset = AssetMapper.ToEntity ( entity );

                context.T_Assets.InsertOnSubmit ( asset );
                    context.SubmitChanges ( );

                entity.ID = asset.Asset_IDPK;
            }
        }
        public void AddAssetList ( IList<IAsset> entities )
        {
            foreach ( IAsset e in entities ) AddAsset ( e );
        }
        public void AddAssetContent ( IAssetContent entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssetContent assetContent = AssetContentMapper.ToEntity ( entity );

                context.T_AssetContents.InsertOnSubmit ( assetContent );
                context.SubmitChanges ( );

                entity.ID = assetContent.AssetContent_IDPK;
            }
        }
        public void AddAssetContentList ( IList<IAssetContent> entities )
        {
            foreach ( IAssetContent e in entities ) AddAssetContent ( e );
        }
        //public void AddAssetContentMetaData ( IAssetContentMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_AssetContentMetaData assetContentMetaData = AssetContentMetaDataMapper.ToEntity ( entity );

        //        context.T_AssetContentMetaDatas.InsertOnSubmit ( assetContentMetaData );
        //        context.SubmitChanges ( );

        //        entity.ID = assetContentMetaData.AssetContentMetaData_IDPK;
        //    }
        //}
        //public void AddAssetContentMetaDataList ( IList<IAssetContentMetaData> entities )
        //{
        //    foreach ( IAssetContentMetaData e in entities ) AddAssetContentMetaData ( e );
        //}
        //public void AddAssetContentRepository ( IAssetContentRepository entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_AssetContentRepository assetContentRepository = AssetContentRepositoryMapper.ToEntity ( entity );
































        //        context.T_AssetContentRepositories.InsertOnSubmit ( assetContentRepository );
        //        context.SubmitChanges ( );

        //        entity.ID = assetContentRepository.AssetContentRepository_IDPK;
        //    }
        //}
        //public void AddAssetContentRepositoryList ( IList<IAssetContentRepository> entities )
        //{
        //    foreach ( IAssetContentRepository e in entities ) AddAssetContentRepository ( e );
        //}












        public void AddAssetContentType ( IAssetContentType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssetContentType assetContentType = AssetContentTypeMapper.ToEntity ( entity );

                context.T_AssetContentTypes.InsertOnSubmit ( assetContentType );
                context.SubmitChanges ( );

                entity.ID = assetContentType.AssetContentType_IDPK;
            }
        }
        public void AddAssetContentTypeList ( IList<IAssetContentType> entities )
        {
            foreach ( IAssetContentType e in entities ) AddAssetContentType ( e );
        }
        //public void AddAssetMetaData ( IAssetMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_AssetMetaData assetMetaData = AssetMetaDataMapper.ToEntity ( entity );

        //        context.T_AssetMetaDatas.InsertOnSubmit ( assetMetaData );
        //        context.SubmitChanges ( );

        //        entity.ID = assetMetaData.AssetMetaData_IDPK;
        //    }
        //}
        //public void AddAssetMetaDataList ( IList<IAssetMetaData> entities )
        //{
        //    foreach ( IAssetMetaData e in entities ) AddAssetMetaData ( e );
        //}



























        public void AddAssetRightsUsage ( IAssetRightsUsage entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssetRightsUsage assetRightsUsage = AssetRightsUsageMapper.ToEntity ( entity );

                context.T_AssetRightsUsages.InsertOnSubmit ( assetRightsUsage );
                context.SubmitChanges ( );

                entity.ID = assetRightsUsage.AssetRightsUsage_IDPK;
            }
        }
        public void AddAssetRightsUsageList ( IList<IAssetRightsUsage> entities )
        {
            foreach ( IAssetRightsUsage e in entities ) AddAssetRightsUsage ( e );
        }
        public void AddAssociatedContent ( IAssociatedContent entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssociatedContent associatedContent = AssociatedContentMapper.ToEntity ( entity );

                context.T_AssociatedContents.InsertOnSubmit ( associatedContent );
                context.SubmitChanges ( );

                entity.ID = associatedContent.AssociatedContent_IDPK;
            }
        }
        public void AddAssociatedContentList ( IList<IAssociatedContent> entities )
        {
            foreach ( IAssociatedContent e in entities ) AddAssociatedContent ( e );
        }
        public void AddAssociatedContentType ( IAssociatedContentType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssociatedContentType associatedContentType = AssociatedContentTypeMapper.ToEntity ( entity );

                context.T_AssociatedContentTypes.InsertOnSubmit ( associatedContentType );
                context.SubmitChanges ( );

                entity.ID = associatedContentType.AssociatedContentType_IDPK;
            }
        }
        public void AddAssociatedContentTypeList ( List<IAssociatedContentType> entities )
        {
            foreach ( IAssociatedContentType e in entities ) AddAssociatedContentType ( e );
        }
        public void AddLanguage ( ILanguage entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Language language = LanguageMapper.ToEntity ( entity );

                context.T_Languages.InsertOnSubmit ( language );
                context.SubmitChanges ( );

                entity.ID = language.Language_IDPK;
            }
        }
        public void AddLanguageList ( IList<ILanguage> entities )
        {
            foreach ( ILanguage e in entities ) AddLanguage ( e );
        }
        //public void AddMetaData ( IMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_MetaData metaData = MetaDataMapper.ToEntity ( entity );

        //        context.T_MetaDatas.InsertOnSubmit ( metaData );
        //        context.SubmitChanges ( );

        //        entity.ID = metaData.MetaData_IDPK;
        //    }
        //}
        //public void AddMetaDataList ( IList<IMetaData> entities )
        //{
        //    foreach ( IMetaData e in entities ) AddMetaData ( e );
        //}
        public void AddMetaKey ( IMetaKey entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_MetaKey metaKey = MetaKeyMapper.ToEntity ( entity );

                context.T_MetaKeys.InsertOnSubmit ( metaKey );
                context.SubmitChanges ( );

                entity.ID = metaKey.MetaKey_IDPK;
            }
        }
        public void AddMetaKeyList ( IList<IMetaKey> entities )
        {
            foreach ( IMetaKey e in entities ) AddMetaKey ( e );
        }
        public void AddMetaValue ( IMetaValue entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_MetaValue metaValue = MetaValueMapper.ToEntity ( entity );

                context.T_MetaValues.InsertOnSubmit ( metaValue );
                context.SubmitChanges ( );

                entity.ID = metaValue.MetaValue_IDPK;
            }
        }
        public void AddMetaValueList ( IList<IMetaValue> entities )
        {
            foreach ( IMetaValue e in entities ) AddMetaValue ( e );
        }
        public void AddMimeType ( IMimeType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_MimeType mimeType = MimeTypeMapper.ToEntity ( entity );

                context.T_MimeTypes.InsertOnSubmit ( mimeType );
                context.SubmitChanges ( );

                entity.ID = mimeType.MimeType_IDPK;
            }
        }
        public void AddMimeTypeList ( IList<IMimeType> entities )
        {
            foreach ( IMimeType e in entities ) AddMimeType ( e );
        }
        public void AddRelatedAsset ( IRelatedAsset entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_RelatedAsset relatedAsset = RelatedAssetMapper.ToEntity ( entity );

                context.T_RelatedAssets.InsertOnSubmit ( relatedAsset );
                context.SubmitChanges ( );

                entity.ID = relatedAsset.RelatedAsset_IDPK;
            }
        }
        public void AddRelatedAssetList ( IList<IRelatedAsset> entities )
        {
            foreach ( IRelatedAsset e in entities ) AddRelatedAsset ( e );
        }
        public void AddRepository ( IRepository entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Repository repository = RepositoryMapper.ToEntity ( entity );

                context.T_Repositories.InsertOnSubmit ( repository );
                context.SubmitChanges ( );

                entity.ID = repository.Repository_IDPK;
            }
        }
        public void AddRepositoryList ( IList<IRepository> entities )
        {
            foreach ( IRepository e in entities ) AddRepository ( e );
        }
        public void AddStatus ( IStatus entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Status status = StatusMapper.ToEntity ( entity );

                context.T_Status.InsertOnSubmit ( status );
                context.SubmitChanges ( );

                entity.ID = status.Status_IDPK;
            }
        }
        public void AddStatusList ( IList<IStatus> entities )
        {
            foreach ( IStatus e in entities ) AddStatus ( e );
        }

        #endregion

        #region delete methods

        public int DeleteAsset ( IAsset entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Asset deleteAsset = AssetMapper.ToEntity ( entity );
                context.T_Assets.Attach ( deleteAsset );

                context.T_Assets.DeleteOnSubmit ( deleteAsset );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssetList ( IList<IAsset> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_Asset> deleteAssetList = ( List<T_Asset> ) AssetMapper.ToEntities ( entities );
                context.T_Assets.AttachAll ( deleteAssetList );

                context.T_Assets.DeleteAllOnSubmit ( deleteAssetList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssetContent ( IAssetContent entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssetContent deleteAssetContent = AssetContentMapper.ToEntity ( entity );
                context.T_AssetContents.Attach ( deleteAssetContent );

                context.T_AssetContents.DeleteOnSubmit ( deleteAssetContent );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssetContentList ( IList<IAssetContent> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_AssetContent> deleteAssetContentList = ( List<T_AssetContent> ) AssetContentMapper.ToEntities ( entities );
                context.T_AssetContents.AttachAll ( deleteAssetContentList );

                context.T_AssetContents.DeleteAllOnSubmit ( deleteAssetContentList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        //public int DeleteAssetContentMetaData ( IAssetContentMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_AssetContentMetaData deleteAssetContentMetaData = AssetContentMetaDataMapper.ToEntity ( entity );
        //        context.T_AssetContentMetaDatas.Attach ( deleteAssetContentMetaData );

        //        context.T_AssetContentMetaDatas.DeleteOnSubmit ( deleteAssetContentMetaData );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        //public int DeleteAssetContentMetaDataList ( IList<IAssetContentMetaData> entities )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        List<T_AssetContentMetaData> deleteAssetContentMetaDataList = ( List<T_AssetContentMetaData> ) AssetContentMetaDataMapper.ToEntities ( entities );
        //        context.T_AssetContentMetaDatas.AttachAll ( deleteAssetContentMetaDataList );

        //        context.T_AssetContentMetaDatas.DeleteAllOnSubmit ( deleteAssetContentMetaDataList );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        //public int DeleteAssetContentRepository ( IAssetContentRepository entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_AssetContentRepository deleteAssetContentRepository = AssetContentRepositoryMapper.ToEntity ( entity );
        //        context.T_AssetContentRepositories.Attach ( deleteAssetContentRepository );

        //        context.T_AssetContentRepositories.DeleteOnSubmit ( deleteAssetContentRepository );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        //public int DeleteAssetContentRepositoryList ( IList<IAssetContentRepository> entities )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        List<T_AssetContentRepository> deleteAssetContentRepositoryList = ( List<T_AssetContentRepository> ) AssetContentRepositoryMapper.ToEntities ( entities );
        //        context.T_AssetContentRepositories.AttachAll ( deleteAssetContentRepositoryList );

        //        context.T_AssetContentRepositories.DeleteAllOnSubmit ( deleteAssetContentRepositoryList );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        public int DeleteAssetContentType ( IAssetContentType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssetContentType deleteAssetContentType = AssetContentTypeMapper.ToEntity ( entity );
                context.T_AssetContentTypes.Attach ( deleteAssetContentType );

                context.T_AssetContentTypes.DeleteOnSubmit ( deleteAssetContentType );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssetContentTypeList ( IList<IAssetContentType> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_AssetContentType> deleteAssetContentTypeList = ( List<T_AssetContentType> ) AssetContentTypeMapper.ToEntities ( entities );
                context.T_AssetContentTypes.AttachAll ( deleteAssetContentTypeList );

                context.T_AssetContentTypes.DeleteAllOnSubmit ( deleteAssetContentTypeList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        //public int DeleteAssetMetaData ( IAssetMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_AssetMetaData deleteAssetMetaData = AssetMetaDataMapper.ToEntity ( entity );
        //        context.T_AssetMetaDatas.Attach ( deleteAssetMetaData );

        //        context.T_AssetMetaDatas.DeleteOnSubmit ( deleteAssetMetaData );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        //public int DeleteAssetMetaDataList ( IList<IAssetMetaData> entities )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        List<T_AssetMetaData> deleteAssetMetaDataList = ( List<T_AssetMetaData> ) AssetMetaDataMapper.ToEntities ( entities );
        //        context.T_AssetMetaDatas.AttachAll ( deleteAssetMetaDataList );

        //        context.T_AssetMetaDatas.DeleteAllOnSubmit ( deleteAssetMetaDataList );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        public int DeleteAssetRightsUsage ( IAssetRightsUsage entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssetRightsUsage deleteAssetRightsUsage = AssetRightsUsageMapper.ToEntity ( entity );
                context.T_AssetRightsUsages.Attach ( deleteAssetRightsUsage );

                context.T_AssetRightsUsages.DeleteOnSubmit ( deleteAssetRightsUsage );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssetRightsUsageList ( IList<IAssetRightsUsage> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_AssetRightsUsage> deleteAssetRightsUsageList = ( List<T_AssetRightsUsage> ) AssetRightsUsageMapper.ToEntities ( entities );
                context.T_AssetRightsUsages.AttachAll ( deleteAssetRightsUsageList );

                context.T_AssetRightsUsages.DeleteAllOnSubmit ( deleteAssetRightsUsageList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssociatedContent ( IAssociatedContent entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssociatedContent deleteAssociatedContent = AssociatedContentMapper.ToEntity ( entity );
                context.T_AssociatedContents.Attach ( deleteAssociatedContent );

                context.T_AssociatedContents.DeleteOnSubmit ( deleteAssociatedContent );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssociatedContentList ( IList<IAssociatedContent> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_AssociatedContent> deleteAssociatedContentList = ( List<T_AssociatedContent> ) AssociatedContentMapper.ToEntities ( entities );
                context.T_AssociatedContents.AttachAll ( deleteAssociatedContentList );

                context.T_AssociatedContents.DeleteAllOnSubmit ( deleteAssociatedContentList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssociatedContentType ( IAssociatedContentType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_AssociatedContentType deleteAssociatedContentType = AssociatedContentTypeMapper.ToEntity ( entity );
                context.T_AssociatedContentTypes.Attach ( deleteAssociatedContentType );

                context.T_AssociatedContentTypes.DeleteOnSubmit ( deleteAssociatedContentType );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteAssociatedContentTypeList ( IList<IAssociatedContentType> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_AssociatedContentType> deleteAssociatedContentTypeList = ( List<T_AssociatedContentType> ) AssociatedContentTypeMapper.ToEntities ( entities );
                context.T_AssociatedContentTypes.AttachAll ( deleteAssociatedContentTypeList );

                context.T_AssociatedContentTypes.DeleteAllOnSubmit ( deleteAssociatedContentTypeList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteLanguage ( ILanguage entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Language deleteLanguage = LanguageMapper.ToEntity ( entity );
                context.T_Languages.Attach ( deleteLanguage );

                context.T_Languages.DeleteOnSubmit ( deleteLanguage );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteLanguageList ( IList<ILanguage> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_Language> deleteLanguageList = ( List<T_Language> ) LanguageMapper.ToEntities ( entities );
                context.T_Languages.AttachAll ( deleteLanguageList );

                context.T_Languages.DeleteAllOnSubmit ( deleteLanguageList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        //public int DeleteMetaData ( IMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        T_MetaData deleteMetaData = MetaDataMapper.ToEntity ( entity );
        //        context.T_MetaDatas.Attach ( deleteMetaData );

        //        context.T_MetaDatas.DeleteOnSubmit ( deleteMetaData );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        //public int DeleteMetaDataList ( IList<IMetaData> entities )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        List<T_MetaData> deleteMetaDataList = ( List<T_MetaData> ) MetaDataMapper.ToEntities ( entities );
        //        context.T_MetaDatas.AttachAll ( deleteMetaDataList );

        //        context.T_MetaDatas.DeleteAllOnSubmit ( deleteMetaDataList );
        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Deletes.Count;
        //    }
        //}
        public int DeleteMetaKey ( IMetaKey entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_MetaKey deleteMetaKey = MetaKeyMapper.ToEntity ( entity );
                context.T_MetaKeys.Attach ( deleteMetaKey );

                context.T_MetaKeys.DeleteOnSubmit ( deleteMetaKey );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteMetaKeyList ( IList<IMetaKey> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_MetaKey> deleteMetaKeyList = ( List<T_MetaKey> ) MetaKeyMapper.ToEntities ( entities );
                context.T_MetaKeys.AttachAll ( deleteMetaKeyList );

                context.T_MetaKeys.DeleteAllOnSubmit ( deleteMetaKeyList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteMetaValue ( IMetaValue entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_MetaValue deleteMetaValue = MetaValueMapper.ToEntity ( entity );
                context.T_MetaValues.Attach ( deleteMetaValue );

                context.T_MetaValues.DeleteOnSubmit ( deleteMetaValue );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteMetaValueList ( IList<IMetaValue> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_MetaValue> deleteMetaValueList = ( List<T_MetaValue> ) MetaValueMapper.ToEntities ( entities );
                context.T_MetaValues.AttachAll ( deleteMetaValueList );

                context.T_MetaValues.DeleteAllOnSubmit ( deleteMetaValueList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteMimeType ( IMimeType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_MimeType deleteMimeType = MimeTypeMapper.ToEntity ( entity );
                context.T_MimeTypes.Attach ( deleteMimeType );

                context.T_MimeTypes.DeleteOnSubmit ( deleteMimeType );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteMimeTypeList ( IList<IMimeType> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_MimeType> deleteMimeTypeList = ( List<T_MimeType> ) MimeTypeMapper.ToEntities ( entities );
                context.T_MimeTypes.AttachAll ( deleteMimeTypeList );

                context.T_MimeTypes.DeleteAllOnSubmit ( deleteMimeTypeList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteRelatedAsset ( IRelatedAsset entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_RelatedAsset deleteRelatedAsset = RelatedAssetMapper.ToEntity ( entity );
                context.T_RelatedAssets.Attach ( deleteRelatedAsset );

                context.T_RelatedAssets.DeleteOnSubmit ( deleteRelatedAsset );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteRelatedAssetList ( IList<IRelatedAsset> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_RelatedAsset> deleteRelatedAssetList = ( List<T_RelatedAsset> ) RelatedAssetMapper.ToEntities ( entities );
                context.T_RelatedAssets.AttachAll ( deleteRelatedAssetList );

                context.T_RelatedAssets.DeleteAllOnSubmit ( deleteRelatedAssetList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteRepository ( IRepository entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Repository deleteRepository = RepositoryMapper.ToEntity ( entity );
                context.T_Repositories.Attach ( deleteRepository );

                context.T_Repositories.DeleteOnSubmit ( deleteRepository );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteRepositoryList ( IList<IRepository> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_Repository> deleteRepositoryList = ( List<T_Repository> ) RepositoryMapper.ToEntities ( entities );
                context.T_Repositories.AttachAll ( deleteRepositoryList );

                context.T_Repositories.DeleteAllOnSubmit ( deleteRepositoryList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteStatus ( IStatus entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                T_Status deleteStatus = StatusMapper.ToEntity ( entity );
                context.T_Status.Attach ( deleteStatus );

                context.T_Status.DeleteOnSubmit ( deleteStatus );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }
        public int DeleteStatusList ( IList<IStatus> entities )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<T_Status> deleteStatusList = ( List<T_Status> ) StatusMapper.ToEntities ( entities );
                context.T_Status.AttachAll ( deleteStatusList );

                context.T_Status.DeleteAllOnSubmit ( deleteStatusList );
                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Deletes.Count;
            }
        }

        #endregion

        #region update methods

        public int UpdateAsset ( IAsset entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var asset = ( from a in context.T_Assets
                              where a.Asset_IDPK == entity.ID
                              select a ).Single ( );

              //  asset.CreationDate_DT = entity.CreationDate;
                asset.CreatorID_NVC = entity.CreatorID;
                asset.LatestEditorID_NVC = entity.LatestEditorID;
                asset.Name_NVC = entity.Name;
                asset.Source_NVC = entity.Source;
                asset.StatusID_FK = entity.StatusID;
                //asset.UpdatedDate_DT = DateTime.Now;
                
                //only add/update don't delete orphans, this is done by the client

                //files
                foreach (IAssetContent content in entity.AssetContentList)
                {
                    //add new file
                    if (content.ID==Int32.MinValue)
                    {
                        content.ID = 0;
                        content.AssetID = asset.Asset_IDPK;
                        AddAssetContent(content);
                    }
                    else
                    {
                        //save metadata
                        var assetContent = (from c in context.T_AssetContents where c.AssetContent_IDPK==content.ID select c).Single();

  
                        assetContent.T_AssetContentMetaDatas.Clear();                         
                        
                        foreach (IMetaData data in content.AssetContentMetaDataList)
                        {
                            context.SP_AssetContentMetadata_AddEdit(assetContent.AssetContent_IDPK, data.MetaKeyID,
                                                                    data.MetaValue.Value, data.MetaValue.LanguageID);
                        }
                        assetContent.LanguageID_FK = content.LanguageID;
                        assetContent.StatusID_FK = content.StatusID;
                    }
                }

                //metadata
                (from assmet in context.T_AssetMetaDatas where assmet.AssetID_FK == asset.Asset_IDPK select assmet)
                    .ToList()
                    .ForEach(assmet => context.T_AssetMetaDatas.DeleteOnSubmit(assmet)); context.SubmitChanges();
//                asset.T_AssetMetaDatas.Clear();context.SubmitChanges ( );
                foreach (IMetaData data in entity.AssetMetaDataList)
                {
                    context.SP_AssetMetadata_AddEdit(asset.Asset_IDPK, data.MetaKeyID, data.MetaValue.Value,
                                                     data.MetaValue.Language.ID);
                }

                //context.SubmitChanges();

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateAssetList ( IList<IAsset> entities )
        {
            int result = 0;

            foreach ( IAsset entity in entities ) result += UpdateAsset ( entity );

            return result;
        }
        public int UpdateAssetContent ( IAssetContent entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from ac in context.T_AssetContents
                          where ac.AssetContent_IDPK == entity.ID
                          select ac ).Single ( );

                a.AssetContentTypeID_FK = entity.AssetContentTypeID;
                a.AssetID_FK = entity.AssetID;
                a.AssetPath_NVC = entity.AssetPath;
            //    a.CreationDate_DT = entity.CreationDate;
                a.CreatorID_NVC = entity.CreatorID;
                a.LanguageID_FK = entity.LanguageID;
                a.LatestEditorID_NVC = entity.LatestEditorID;
                a.MimeTypeID_FK = entity.MimeTypeID;
                a.StatusID_FK = entity.StatusID;
           //     a.UpdatedDate_DT = DateTime.Now;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateAssetContentList ( IList<IAssetContent> entities )
        {
            int result = 0;

            foreach ( IAssetContent entity in entities ) result += UpdateAssetContent ( entity );

            return result;
        }
        //public int UpdateAssetContentMetaData(IAssetContentMetaData entity)
        //{
        //    using (context = DataContextFactory.CreateContext())
        //    {
        //        var a = (from acmd in context.T_AssetContentMetaDatas
        //                 where acmd.AssetContentMetaData_IDPK == entity.ID
        //                 select acmd).Single();

        //        a.AssetContentID_FK = entity.AssetContentID;
        //        a.MetaDataID_FK = entity.MetaDataID;
        //        a.StatusID_FK = entity.StatusID;

        //        context.SubmitChanges();

        //        return context.GetChangeSet().Updates.Count;
        //    }
        //}
        //public int UpdateAssetContentMetaDataList ( IList<IAssetContentMetaData> entities )
        //{
        //    int result = 0;

        //    foreach ( IAssetContentMetaData entity in entities ) result += UpdateAssetContentMetaData ( entity );

        //    return result;
        //}
        //public int UpdateAssetContentRepository ( IAssetContentRepository entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        var a = ( from acr in context.T_AssetContentRepositories
        //                  where acr.AssetContentRepository_IDPK == entity.ID
        //                  select acr ).Single ( );

        //        a.AssetContentID_FK = entity.AssetContentID;
        //        a.RepositoryID_FK = entity.RepositoryID;
        //        a.StatusID_FK = entity.StatusID;

        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Updates.Count;
        //    }
        //}
        //public int UpdateAssetContentRepositoryList ( IList<IAssetContentRepository> entities )
        //{
        //    int result = 0;

        //    foreach ( IAssetContentRepository entity in entities ) result += UpdateAssetContentRepository ( entity );

        //    return result;
        //}
        public int UpdateAssetContentType ( IAssetContentType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from act in context.T_AssetContentTypes
                          where act.AssetContentType_IDPK == entity.ID
                          select act ).Single ( );

              //  a.DateModified_DT = DateTime.Now;
                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;
                a.Height_DC = entity.Height;
                a.Width_DC = entity.Width;
                a.Name_NVC = entity.Name;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateAssetContentTypeList ( IList<IAssetContentType> entities )
        {
            int result = 0;

            foreach ( IAssetContentType entity in entities ) result += UpdateAssetContentType ( entity );

            return result;
        }
        //public int UpdateAssetMetaData ( IAssetMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        var a = ( from amd in context.T_AssetMetaDatas
        //                  where amd.AssetMetaData_IDPK == entity.ID
        //                  select amd ).Single ( );

        //        a.AssetID_FK = entity.AssetID;
        //        a.MetaDataID_FK = entity.MetaDataID;
        //        a.StatusID_FK = entity.StatusID;

        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Updates.Count;
        //    }
        //}
        //public int UpdateAssetMetaDataList ( IList<IAssetMetaData> entities )
        //{
        //    int result = 0;

        //    foreach ( IAssetMetaData entity in entities ) result += UpdateAssetMetaData ( entity );

        //    return result;
        //}
        public int UpdateAssetRightsUsage ( IAssetRightsUsage entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from aru in context.T_AssetRightsUsages
                          where aru.AssetRightsUsage_IDPK == entity.ID
                          select aru ).Single ( );

                a.AssetID_FK = entity.AssetID;
                a.EndPublishDate_DT = entity.EndPublishDate;
                a.Group_NVC = entity.Group;
                a.StartPublishDate_DT = entity.StartPublishDate;
                a.StatusID_FK = entity.StatusID;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateAssetRightsUsageList ( IList<IAssetRightsUsage> entities )
        {
            int result = 0;

            foreach ( IAssetRightsUsage entity in entities ) result += UpdateAssetRightsUsage ( entity );

            return result;
        }
        public int UpdateAssociatedContent ( IAssociatedContent entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from ac in context.T_AssociatedContents
                          where ac.AssociatedContent_IDPK == entity.ID
                          select ac ).Single ( );

                a.AssetID_FK = entity.AssetID;
                a.AssociatedContentTypeID_FK = entity.AssociatedContentTypeID;
                a.ContentValue_NVC = entity.ContentValue;
              //  a.CreationDate_DT = entity.CreationDate;
                a.StatusID_FK = entity.StatusID;
              //  a.UpdatedDate_DT = DateTime.Now;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateAssociatedContentList ( IList<IAssociatedContent> entities )
        {
            int result = 0;

            foreach ( IAssociatedContent entity in entities ) result += UpdateAssociatedContent ( entity );

            return result;
        }
        public int UpdateAssociatedContentType ( IAssociatedContentType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from act in context.T_AssociatedContentTypes
                          where act.AssociatedContentType_IDPK == entity.ID
                          select act ).Single ( );

                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;
                a.Name_NVC = entity.Name;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateAssociatedContentTypeList ( IList<IAssociatedContentType> entities )
        {
            int result = 0;

            foreach ( IAssociatedContentType entity in entities ) result += UpdateAssociatedContentType ( entity );

            return result;
        }
        public int UpdateLanguage ( ILanguage entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from l in context.T_Languages
                          where l.Language_IDPK == entity.ID
                          select l ).Single ( );

                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;
                a.LanguageCode_NVC = entity.LanguageCode;
                a.Name_NVC = entity.Name;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateLanguageList ( IList<ILanguage> entities )
        {
            int result = 0;

            foreach ( ILanguage entity in entities ) result += UpdateLanguage ( entity );

            return result;
        }
        //public int UpdateMetaData ( IMetaData entity )
        //{
        //    using ( context = DataContextFactory.CreateContext ( ) )
        //    {
        //        var a = ( from md in context.T_MetaDatas
        //                  where md.MetaData_IDPK == entity.ID
        //                  select md ).Single ( );

        //        a.CreatedDate_DT = entity.CreatedDate;
        //        a.CreatorID_NVC = entity.CreatorID;
        //        a.LatestEditorID_NVC = entity.LatestEditorID;
        //        a.MetaKeyID_FK = entity.MetaKeyID;
        //        a.MetaValueID_FK = entity.MetaValueID;
        //        a.UpdatedDate_DT = DateTime.Now;

        //        context.SubmitChanges ( );

        //        return context.GetChangeSet ( ).Updates.Count;
        //    }
        //}
        //public int UpdateMetaDataList ( IList<IMetaData> entities )
        //{
        //    int result = 0;

        //    foreach ( IMetaData entity in entities ) result += UpdateMetaData ( entity );

        //    return result;
        //}
        public int UpdateMetaKey ( IMetaKey entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from mk in context.T_MetaKeys
                          where mk.MetaKey_IDPK == entity.ID
                          select mk ).Single ( );

                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;
                a.Name_NVC = entity.Name;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateMetaKeyList ( IList<IMetaKey> entities )
        {
            int result = 0;

            foreach ( IMetaKey entity in entities ) result += UpdateMetaKey ( entity );

            return result;
        }
        public int UpdateMetaValue ( IMetaValue entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from mv in context.T_MetaValues
                          where mv.MetaValue_IDPK == entity.ID
                          select mv ).Single ( );

                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;

                // TODO:
                // fix the bug which prevents the LanguageID from being changed
                // and updated.
                //a.LanguageID_FK = entity.LanguageID;
                entity.Value = entity.Value;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateMetaValueList ( IList<IMetaValue> entities )
        {
            int result = 0;

            foreach ( IMetaValue entity in entities ) result += UpdateMetaValue ( entity );

            return result;
        }
        public int UpdateMimeType ( IMimeType entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from mt in context.T_MimeTypes
                          where mt.MimeType_IDPK == entity.ID
                          select mt ).Single ( );

                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;
                a.Name_NVC = entity.Name;
                a.MimeType_NVC = entity._MimeType;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateMimeTypeList ( IList<IMimeType> entities )
        {
            int result = 0;

            foreach ( IMimeType entity in entities ) result += UpdateMimeType ( entity );

            return result;
        }

        // TODO: 
        // fix the bug in this code. Appears that constraints on the database 
        // or the DBML file behind the context has some issue which prevents
        // the database from updating the RelatedAsset.
        public int UpdateRelatedAsset ( IRelatedAsset entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from ra in context.T_RelatedAssets
                          where ra.RelatedAsset_IDPK == entity.ID
                          select ra ).Single ( );

                a.AssetID_FK = entity.AssetID;
             //   a.CreationDate_DT = entity.CreatedDate;
                a.RelatedAssetID_FK = entity.RelatedAssetID;
                a.StatusID_FK = entity.StatusID;
             //   a.UpdatedDate_DT = DateTime.Now;

                try
                {
                    context.SubmitChanges ( );
                }
                catch ( Exception ex ) { }
                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateRelatedAssetList ( IList<IRelatedAsset> entities )
        {
            int result = 0;

            foreach ( IRelatedAsset entity in entities ) result += UpdateRelatedAsset ( entity );

            return result;
        }
        public int UpdateRepository ( IRepository entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from r in context.T_Repositories
                          where r.Repository_IDPK == entity.ID
                          select r ).Single ( );

                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;
                a.Name_NVC = entity.Name;
                a.URLPrefix_NVC = entity.URLPrefix;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateRepositoryList ( IList<IRepository> entities )
        {
            int result = 0;

            foreach ( IRepository entity in entities ) result += UpdateRepository ( entity );

            return result;
        }
        public int UpdateStatus ( IStatus entity )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                var a = ( from s in context.T_Status
                          where s.Status_IDPK == entity.ID
                          select s ).Single ( );

                a.Description_NVC = entity.Description;
                a.Enabled_BIT = entity.Enabled;
                a.Name_NVC = entity.Name;

                context.SubmitChanges ( );

                return context.GetChangeSet ( ).Updates.Count;
            }
        }
        public int UpdateStatusList ( IList<IStatus> entities )
        {
            int result = 0;

            foreach ( IStatus entity in entities ) result += UpdateStatus ( entity );

            return result;
        }

        #endregion

        #endregion

        #region IAssetReaderFactory Members


        public IMetaKey GetMetaKeyByID(int id)
        {
            throw new NotImplementedException();
        }

        public IMetaValue GetMetaValueByID(int id)
        {
            throw new NotImplementedException();
        }

        public IMimeType GetMimeTypeByID(int id)
        {
            throw new NotImplementedException();
        }

        public IRelatedAsset GetRelatedAssetByID(int id)
        {
            throw new NotImplementedException();
        }

        public IList<IRelatedAsset> GetRelatedAssetByAssetID(int assetId)
        {
            throw new NotImplementedException();
        }

        public IRepository GetRepositoryByID(int id)
        {
            throw new NotImplementedException();
        }

        public IStatus GetStatusByID(int id)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
